﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ToolKitIESiteList.Library;

namespace ToolKitManagerIESiteList
{
    public partial class FrmTracker : Form
    {

        [SuppressMessage("Microsoft.Usage", "CA2227")]
        public ReadOnlyCollection<HotTracker> DataTracker { get; set; }
        private bool Resizing = false;


        public FrmTracker()
        {
            InitializeComponent();
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            Close();
        }


        private void ListView_SizeChanged(object sender, EventArgs e)
        {
            // Don't allow overlapping of SizeChanged calls
            if (!Resizing)
            {
                // Set the resizing flag
                Resizing = true;

                ListView listView = sender as ListView;
                if (listView != null)
                {
                    float totalColumnWidth = 0;

                    // Get the sum of all column tags
                    for (int i = 0; i < listView.Columns.Count; i++)
                        totalColumnWidth += Convert.ToInt32(listView.Columns[i].Tag);

                    // Calculate the percentage of space each column should 
                    // occupy in reference to the other columns and then set the 
                    // width of the column to that percentage of the visible space.
                    for (int i = 0; i < listView.Columns.Count; i++)
                    {
                        float colPercentage = (Convert.ToInt32(listView.Columns[i].Tag) / totalColumnWidth);
                        listView.Columns[i].Width = (int)(colPercentage * listView.ClientRectangle.Width);
                    }
                }
            }
            // Clear the resizing flag
            Resizing = false;
        }


        private void FrmTracker_Load(object sender, EventArgs e)
        {
            LblTitle.Text = Properties.Resources.Summary;
            listViewDetails.Visible = false;
            listViewDataTracker.Visible = true;
            listViewDataTracker.SuspendLayout();
            listViewDataTracker.Items.Clear();
            foreach (var item in DataTracker)
            {
                ListViewItem lvi = new ListViewItem(item.DtRef.ToString());
                lvi.Tag = item.id;
                lvi.SubItems.Add(string.Format("{0}:{1}", item.Action.ToString(),item.ActionDescription));
                lvi.SubItems.Add(item.ActionComplement);
                lvi.SubItems.Add(item.Count.ToString());
                listViewDataTracker.Items.Add(lvi);
            }
            ListView_SizeChanged(listViewDataTracker, e);
            BntBack.Visible = false;
            if (DataTracker.Count == 0)
            {
                bntPrint.Enabled = false;
                BntSave.Enabled = false;
            }
        }

        private void listViewDataTraker_DoubleClick(object sender, EventArgs e)
        {
            if (listViewDataTracker.SelectedItems.Count == 0) return;

            var id = listViewDataTracker.SelectedItems[0].Tag.ToString();
            var sites = DataTracker.First(d => d.id == id).Sites;

            if (sites.Count == 0) return;

            ListView_SizeChanged(listViewDetails, e);
            listViewDetails.Items.Clear();
            foreach (var item in sites)
            {
                AddItem(item);
            }
            listViewDetails.Visible = true;
            listViewDataTracker.Visible = false;
            BntBack.Visible = true;
            BntSave.Enabled = false;
            bntPrint.Enabled = false;
            LblTitle.Text = string.Format("{0}:{1}",
                listViewDataTracker.SelectedItems[0].SubItems[1].Text,
                listViewDataTracker.SelectedItems[0].SubItems[2].Text);
        }

        private void AddItem(DataTraker item)
        {
            ListViewItem lvi = new ListViewItem(string.Format("{0}{1}",item.HostPort,item.Path));
            lvi.SubItems.Add(item.CompatMode.ToString());
            lvi.SubItems.Add(item.OpenIn.ToString());
            listViewDetails.Items.Add(lvi);
        }

        private void BntBack_Click(object sender, EventArgs e)
        {
            listViewDetails.Visible = false;
            listViewDataTracker.Visible = true;
            BntBack.Visible = false;
            BntSave.Enabled = true;
            bntPrint.Enabled = true;
            LblTitle.Text = Properties.Resources.Summary;
        }

        private void FrmTracker_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.None)
            {
                e.Cancel = true;
            }
        }

        private void BntSave_Click(object sender, EventArgs e)
        {
            saveFileDialog.FileName = string.Empty;
            saveFileDialog.ShowDialog();
        }

        private void bntPrint_Click(object sender, EventArgs e)
        {
            var fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xml";
            var printName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
            if (!string.IsNullOrEmpty(EmieDocument.FileNameEmieDocument))
            {
                printName = Path.GetFileNameWithoutExtension(EmieDocument.FileNameEmieDocument);
                printName = Path.Combine(Path.GetTempPath() + printName + ".html");

            }
            ManagerHotTracker.SaveTofile(fileName);
            ManagerHotTracker.ConverToHtml(fileName, printName, EmieDocument.VersionEmieDocument.ToString(), DateTime.Now.ToString());
            System.Diagnostics.Process.Start(printName);
        }
    }
}
